<?php

namespace App\Admin\Controllers;

use App\Models\Chat;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Database\Eloquent\SoftDeletes;

class ChatController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('聊天记录');
            $content->description('description');

            $content->body($this->grid());
        });
    }
    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Chat::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            //$grid->model()->withTrashed();
            $grid->disableCreateButton();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
            });
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 在这里添加字段过滤器
                $filter->like('user_name', '用户');
                $filter->between('time', '时间');
            });
            $grid->user_name('用户');
            $grid->user_headpic('头像')->image('/uploads/', 36, 36);
            $grid->txt('内容')->display(function($txt) {
                return $txt;
            });
            $grid->time()->display(function ($time) {
                return date('Y-m-d H:i:s',$time);
            })->sortable();
        });
    }
    protected function form()
    {
        return Admin::form(Chat::class, function (Form $form) {
            //$form->editor('txt','内容');
            $form->display('id', 'ID');
        });
    }
}
